package cn.yqn.system.controller;

import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.system.domain.Permission;
import cn.yqn.system.query.PermissionQuery;
import cn.yqn.system.service.IPermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：菜单
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    public IPermissionService permissionService;

    /**
     * 添加或修改
     * @param permission  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Permission permission){
        try {
            if( permission.getId()!=null)
                permissionService.update(permission);
            else
                permissionService.save(permission);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("操作失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            permissionService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            permissionService.patchDel(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("删除失败");
        }
    }


    /**
     * 查询单个信息
     * @param id
     */
    @GetMapping("/{id}")
    public Permission findOne(@PathVariable("id")Long id) {
        return permissionService.loadById(id);
    }


    /**
     * 查询所有信息
     * @return
     */
    @GetMapping
    public List<Permission> findAll(){
        return permissionService.loadAll();
    }


    /**
     * 分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Permission> queryPage(@RequestBody PermissionQuery query) {
        return permissionService.pageQuery(query);
    }



}
